package com.devicemantra.coordinate.ds.tests;
import com.devicemantra.coordinate.dataobjects.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.*;

import com.devicemantra.coordinate.dataobjects.CDepartmentFactory;
import com.devicemantra.coordinate.exceptions.operations.CDepartmentDoesNotExistsException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

@SuppressWarnings("serial")
public class AddSubDepartment extends HttpServlet 
{
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException 
	{
		//resp.setContentType("text/html");
		String lDeptName = (String) req.getParameter("dept_name");
		CDepartment lDept = null;
		try {
			lDept = CDepartmentFactory.getDepartment(lDeptName);
		} catch (CDepartmentDoesNotExistsException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String lSubDeptName = (String) req.getParameter("sub_dept_name");
		System.out.println("Department Name = " + lDeptName + "SD = " + lSubDeptName);
		PersistenceManager pm = PMF.get().getPersistenceManager();
		//Department lDept = (Department) pm.getObjectById(Department.class,KeyFactory.stringToKey(lKey));
		System.out.println("SettingAttribute() to " + lDept.getDepartmentName());
		//resp.getOutputStream().println("DEPARTMENT FOUND IS " + lDept.getDepartmentName());
		SubDepartment lNewSubDept = SubDepartmentFactory.createNewSubDepartment(lDept.getDepartmentName(), lSubDeptName);
		
		//resp.getOutputStream().println("NEW SUB DEPARTMENT  IS " + lNewSubDept.getSubDepartmentName());
		int lResult = lDept.addSubDepartment(lNewSubDept);
		System.out.println("SettingAttribute() to " + lDept.getDepartmentName());
		req.setAttribute("DEPT", lDept.getDepartmentName());
		if(lResult == -1)
		{
			System.out.println("ERROR : Sub department already exists");
			return;
		}
		else
		{
			pm.makePersistent(lDept);
			pm.close();
			System.out.println("SUCCESS : Sub department Added ");
			
		}
		
		/* Added */
		List<SubDepartment> lListOfSubDepts = SubDepartmentFactory.getAllSubDepartments(lDeptName);
		Iterator<SubDepartment> lSubDepartmentIter = lListOfSubDepts.iterator();
		resp.getOutputStream().print("<TABLE BORDER=0 COLOR=00FF00>");
		int counter = 3;
		while( lSubDepartmentIter.hasNext())
		{
			 if( (counter % 3 ) == 0)   
			 {
				 resp.getOutputStream().print("<TR>");
				 	
			 }
				 counter++;
				 SubDepartment lSubDept = lSubDepartmentIter.next();
				 resp.getOutputStream().print("<TD><input type=\"checkbox\" name=\"vehicle\" value=\"Car\" DISABLED />" + lSubDept.getSubDepartmentName() +  "</TD>");
				 
				 if( (counter % 3 ) == 0)
				 {
					 resp.getOutputStream().print("</TR>");
				 } 
				 
		}
		
		resp.getOutputStream().print("</TABLE><br><br>");
		resp.getOutputStream().print("<div id=\"temp\">" + "</div>");
		
		
	 /*try
		{
			System.out.println("FORWARDING TO :/modules/administration/getWindow.jsp?category=subdepartment");
			//req.getRequestDispatcher("/modules/administration/subdepartment.jsp").forward(req,resp);
			req.getRequestDispatcher("/modules/administration/getWindow.jsp?category=subdepartment").forward(req,resp);
		} catch(Exception e) {} */
	}
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
	throws IOException 
	{
		//doPost(req,resp);
		String lKey = (String) req.getParameter("dept_name");
		String lSubDeptName = (String) req.getParameter("sub_dept_name");
		System.out.println("lKey = " + lKey + "SDS = " + lSubDeptName);
		doPost(req,resp);
	}
}
